package ch.ethz.group3.myalbum.client;

import java.util.ArrayList;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public class TopBarView extends ToolStrip {

	private ArrayList<EnterListener> listeners;
	private final DynamicForm form;

	public TopBarView() {
		this.listeners = new ArrayList<EnterListener>();
		setHeight(40);
		setWidth100();
		addSpacer(6);

		Img bookImage = new Img();
		bookImage.setSrc("fgf_logo64.png");
		bookImage.setWidth("35px");
		bookImage.setHeight("35px");
		bookImage.setShowRollOver(false);
		bookImage.setShowDownIcon(false);
		bookImage.setShowDown(false);

		bookImage.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				SC.say("About",
						"WebEngineering2012 - Flickr Groups Finder<br/><br/>Created by:<br/><br/>Thomas, Giulio and Stefano");
			}
		});

		addMember(bookImage);
		addSpacer(5);

		Label title = new Label("Flickr Groups Finder");
		title.setStyleName("title");
		title.setWidth(300);
		addMember(title);

		addFill();
		addSeparator();

		form = new DynamicForm();
		form.setAlign(Alignment.RIGHT);
		form.setLayoutAlign(Alignment.RIGHT);
		form.setLayoutAlign(VerticalAlignment.CENTER);
		form.setAutoHeight();
		form.setWidth("240px");
		
		final TextItem search = new TextItem();
		search.setTitle("Search Groups");
		search.setDefaultValue("Lugano");
		

		search.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					fireSearchAction(search.getValue() + "");
				}
			}
		});
		
		form.setFields(search);
		addMember(form);
		addSpacer(20);
	}

	public void addListener(EnterListener li) {
		this.listeners.add(li);
	}

	public void removeListener(EnterListener li) {
		this.listeners.remove(li);
	}

	private void fireSearchAction(String s) {
		for (EnterListener li : listeners)
			li.search(s);
	}

}